package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysConvention;
import com.ruoyi.system.service.ISysConventionService;
import com.siashan.sensitive.SensitiveCheck;
import com.siashan.sensitive.SensitiveCheckStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 系统约定
 * Controller
 *
 * @author siashan
 * @date 2023-05-25
 */
@RestController
@RequestMapping("/system/convention")
public class SysConventionController extends BaseController {

    @Autowired
    private ISysConventionService sysConventionService;

    /**
     * 查询系统约定列表
     *
     * @param sysConvention 参数
     * @return 列表
     */
    @PreAuthorize("@ss.hasPermi('system:convention:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysConvention sysConvention) {
        startPage();
        LambdaQueryWrapper<SysConvention> lqw = new LambdaQueryWrapper<SysConvention>();
        if (StringUtils.isNotBlank(sysConvention.getRuleName())) {
            lqw.like(SysConvention::getRuleName, sysConvention.getRuleName());
        }
        if (sysConvention.getRuleType() != null) {
            lqw.eq(SysConvention::getRuleType, sysConvention.getRuleType());
        }
        List<SysConvention> list = sysConventionService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出系统约定
     * 列表
     *
     * @param sysConvention
     * @return 导出结果
     */
    @PreAuthorize("@ss.hasPermi('system:convention:export')")
    @Log(title = "系统约定", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysConvention sysConvention) {
        LambdaQueryWrapper<SysConvention> lqw = new LambdaQueryWrapper<SysConvention>(sysConvention);
        List<SysConvention> list = sysConventionService.list(lqw);
        ExcelUtil<SysConvention> util = new ExcelUtil<SysConvention>(SysConvention.class);
        return util.exportExcel(list, "convention");
    }

    /**
     * 获取系统约定详细信息
     *
     * @param id id
     * @return 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:convention:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysConventionService.getById(id));
    }

    /**
     * 新增系统约定
     *
     * @param sysConvention 参数
     * @return 结果
     */
    @SensitiveCheck
    @PreAuthorize("@ss.hasPermi('system:convention:add')")
    @Log(title = "系统约定", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @SensitiveCheck(type = SensitiveCheckStrategy.REPLACE_WORD) SysConvention sysConvention) {
        return toAjax(sysConventionService.save(sysConvention) ? 1 : 0);
    }

    /**
     * 修改系统约定
     *
     * @param sysConvention 参数
     * @return 结果
     */
    @SensitiveCheck
    @PreAuthorize("@ss.hasPermi('system:convention:edit')")
    @Log(title = "系统约定", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @SensitiveCheck(type = SensitiveCheckStrategy.REPLACE_WORD) SysConvention sysConvention) {
        return toAjax(sysConventionService.updateById(sysConvention) ? 1 : 0);
    }

    /**
     * 删除系统约定
     *
     * @param ids 主键id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:convention:remove')")
    @Log(title = "系统约定", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysConventionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
